package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.article.SortAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.share.ArticleType;
import com.magicsoft.share.presenter.article.SortPresenter;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.article.ISortView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SortActivity extends BaseActivity<ISortView, SortPresenter> implements ISortView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private SortAdapter sortAdapter;

    @Override
    public SortPresenter initPresenter() {
        return new SortPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_sort;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvTitle.setText("分类");
        initRV();
    }

    @Override
    public void requestData() {

        mPresenter.articleTypeList();
    }

    @OnClick(R.id.ibtn_left)
    public void onViewClicked() {
        Intent intent = new Intent();
        if(sortAdapter.getItem(sortAdapter.getSelectPosition())!=null){
            intent.putExtra("articleType", sortAdapter.getItem(sortAdapter.getSelectPosition()).getId());
            intent.putExtra("articleTypeStr", sortAdapter.getItem(sortAdapter.getSelectPosition()).getType_name());
            setResult(RESULT_OK, intent);
            finish();
        }else {
            ToastUtils.showToast(this,"请选择分类的类型");
        }

    }

    private void initRV() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(this, 0.8f)));

        sortAdapter = new SortAdapter(R.layout.item_choose);
        sortAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sortAdapter.setSelectPosition(position);
                sortAdapter.notifyDataSetChanged();
                Intent intent = new Intent();
                intent.putExtra("articleType", sortAdapter.getItem(sortAdapter.getSelectPosition()).getId());
                intent.putExtra("articleTypeStr", sortAdapter.getItem(sortAdapter.getSelectPosition()).getType_name());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        recyclerView.setAdapter(sortAdapter);

    }

    @Override
    public void articleTypeListSuccess(List<ArticleType> articleTypes) {
        articleTypes.remove(0);
        sortAdapter.setNewData(articleTypes);
    }
}
